﻿using System;

namespace Microsoft.Office.PowerPoint.STAMP.Core
{
    /// <summary>
    /// Represents a length to be applied to a caption property..
    /// </summary>
    public class Length : IEquatable<Length>
    {
        /// <summary>
        /// The numeric value of this length.
        /// </summary>
        public float Value { get; set; }

        /// <summary>
        /// The unit of this length.
        /// </summary>
        public LengthUnit Unit { get; set; }

        /// <summary>
        /// Returns the size in points.
        /// </summary>
        /// <param name="containerLength">Length of the container.</param>
        /// <returns></returns>
        public float ToPointSize(double containerLength = 0)
        {
            if (Unit == LengthUnit.Percent)
                return (float) (Value * containerLength);
            return Value;
        }

        /// <summary>
        /// Indicates whether the current object is equal to another object of the same type.
        /// </summary>
        /// <param name="other">An object to compare with this object.</param>
        /// <returns>
        /// true if the current object is equal to the <paramref name="other"/> parameter; otherwise, false.
        /// </returns>
        public bool Equals(Length other)
        {
            if (ReferenceEquals(null, other))
            {
                return false;
            }
            return (other.Unit == Unit && other.Value == Value);
        }

        /// <summary>
        /// Determines whether the specified <see cref="System.Object"/> is equal to this instance.
        /// </summary>
        /// <param name="obj">The <see cref="System.Object"/> to compare with this instance.</param>
        /// <returns>
        ///   <c>true</c> if the specified <see cref="System.Object"/> is equal to this instance; otherwise, <c>false</c>.
        /// </returns>
        public override bool Equals(object obj)
        {
            if (ReferenceEquals(null, obj)) return false;
            if (ReferenceEquals(this, obj)) return true;
            if (obj.GetType() != typeof (Length)) return false;
            return Equals((Length) obj);
        }

        public override int GetHashCode()
        {
            return Value.GetHashCode() & Unit.GetHashCode();
        }

        /// <summary>
        /// Implements the operator ==.
        /// </summary>
        public static bool operator ==(Length left, Length right)
        {
            return Equals(left, right);
        }

        /// <summary>
        /// Implements the operator !=.
        /// </summary>
        public static bool operator !=(Length left, Length right)
        {
            return !Equals(left, right);
        }

        /// <summary>
        /// Returns a <see cref="System.String"/> that represents this instance.
        /// </summary>
        public override string ToString()
        {
            return Value + (Unit == LengthUnit.Point ? "Em" : "%").ToLower();
        }
    }
}